
// 球球直径
const diameter = 130;
cc.Class({
    extends: cc.Component,

    properties: {
        gameNode:cc.Node,
        monsterNode:cc.Node,
        player:cc.Node,
        monsterSpriteFrame:cc.SpriteFrame,
        timeLabel:cc.Label,
        gameOverLabel:cc.Label,
        refreshButton:cc.Node,
    },

    start () {
        this.motionX = 0;
        this.motionY = 0;
        cc.systemEvent.setAccelerometerEnabled(true);
        cc.systemEvent.setAccelerometerInterval(1/60);
        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
        this.monsterList = [];
        this.timeValue = 0;
        this.gaming = true;
        this.firstMonster = true;
    },
    onDestroy () {
        cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    },
    onDeviceMotionEvent (event) {
        this.motionX = event.acc.x;
        this.motionY = event.acc.y;
    },
    update (dt) {
        this.updateForMovePlayer(dt);
        this.updateForMoveMonster(dt);
        this.updateForGenMonster(dt);
        this.updateForTime(dt);
    },
    // 刷新时间
    updateForTime (dt) {
        if (!this.gaming) return;
        this.timeValue += dt;
        this.timeLabel.string = "坚持" + Math.floor(this.timeValue) + "秒";
    },
    // 移动玩家
    updateForMovePlayer (dt) {
        if (!this.gaming) return;
        const speed = 3000;
        var x = this.player.x + this.motionX*dt*speed;
        var y = this.player.y + this.motionY*dt*speed;

        this.player.x = Math.min(this.gameNode.width/2, Math.max(-this.gameNode.width/2, x));
        this.player.y = Math.min(this.gameNode.height/2, Math.max(-this.gameNode.height/2, y));
    },
    // 产生新的怪物
    updateForGenMonster (dt) {
        if (!this.gaming) return;
        if (Math.random() < 0.01 + Math.sqrt(this.timeValue) * 0.003 || this.firstMonster) {
            this.firstMonster = false;
            var monsterNode = new cc.Node();
            var monsterSprite = monsterNode.addComponent(cc.Sprite);
            monsterSprite.spriteFrame = this.monsterSpriteFrame;
            if (Math.random() > this.gameNode.width/(this.gameNode.height+this.gameNode.width)) {
                monsterNode.x = Math.random() * this.gameNode.width - this.gameNode.width/2;
                monsterNode.y = (Math.random()>0.5?1:-1) * (this.gameNode.height/2 + diameter/2);
            } else {
                monsterNode.y = Math.random() * this.gameNode.height - this.gameNode.height/2;
                monsterNode.x = (Math.random()>0.5?1:-1) * (this.gameNode.width/2 + diameter/2);
            }
            monsterNode.moveDt = this.player.position.subSelf(monsterNode.position).normalizeSelf();
            monsterNode.parent = this.monsterNode;
            this.monsterList.push(monsterNode);
        }
    },
    // 更新怪物位置
    updateForMoveMonster (dt) {
        if (!this.gaming) return;
        const speed = 200;
        var outMonster = null;
        for (var i = 0; i < this.monsterList.length; i++) {
            this.monsterList[i].x += this.monsterList[i].moveDt.x * dt * speed;
            this.monsterList[i].y += this.monsterList[i].moveDt.y * dt * speed;
            // 判断出屏幕
            if (outMonster !== null
                && this.monsterList[i].x > this.monsterNode.width/2 + diameter
                || this.monsterList[i].x < -this.monsterNode.width/2 - diameter
                || this.monsterList[i].y > this.monsterNode.height/2 + diameter
                || this.monsterList[i].y < -this.monsterNode.height/2 - diameter ) {

                outMonster = i;
            }
            // 判断gameover
            if (
                Math.abs(this.monsterList[i].x - this.player.x) < diameter
                && Math.abs(this.monsterList[i].y - this.player.y) < diameter
                ){
                if (
                    Math.pow(this.monsterList[i].x - this.player.x, 2)
                    +
                    Math.pow(this.monsterList[i].y - this.player.y, 2)
                    < diameter*diameter
                    ){
                    // game over
                    console.log("gameover");
                    this.gameOver();
                    return;
                }
            }
        }
        if (outMonster !== null) {
            var removeMonster = this.monsterList[outMonster];
            removeMonster.removeFromParent();
            this.monsterList.splice(outMonster, 1);
        }
    },
    // 死了
    gameOver () {
        this.gaming = false;
        this.gameOverLabel.node.runAction(cc.sequence(
            cc.delayTime(0.5),
            cc.callFunc(()=>{
                this.gameOverLabel.string = "坚持了"+ Math.floor(this.timeValue) + "秒";
                this.timeLabel.node.y = 1000;
                this.gameOverLabel.node.y = 0;
            }),
            cc.scaleTo(0.5, 2, 2),
            cc.callFunc(()=>{
                this.refreshButton.y = -cc.winSize.height/4;
            }),
        ));
    },
    // 再玩一次
    restarGame () {
        this.monsterNode.removeAllChildren();
        this.monsterList = [];
        this.timeLabel.node.y = 0;
        this.gameOverLabel.node.y = 1000;
        this.gameOverLabel.node.setScale(1,1);
        this.refreshButton.y = 1000;
        this.timeValue = 0;
        this.gaming = true;
        this.firstMonster = true;
    }
});
